/********************************************************************************
 * BannerAdvertisersAdminAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Dec 2, 2008 4:47:53 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.BannedUser;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.SearchCriteria;

public class BlockUserAdminAction extends AdminEngineManagement implements ServletRequestAware {

	/** */
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;
	private List<BannedUser> bannedUserList = new ArrayList<BannedUser>();

	private BannedUser bannedUser;

	private String user;

	private String findUser;

	private int pageSize = 0;

	public BannedUser getBannedUser() {
		return bannedUser;
	}

	public void setBannedUser(BannedUser bannedUser) {
		this.bannedUser = bannedUser;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	@SuppressWarnings("unchecked")
	public String execute() throws Exception {
		if(this.pageSize <= 0)
			this.pageSize = 10;
		this.bannedUserList = (List<BannedUser>) CommonFunctionDelegator.getAllfromtable(BannedUser.class.getName());
		return SUCCESS;
	}

	public String findBlockUser() throws Exception {
		if(this.pageSize <= 0)
			this.pageSize = 10;

		if(this.findUser != null && this.findUser.trim().length() > 0) {
			User user = UserServiceDelegator.getUserByUserId(this.findUser);
			if(user != null) {
				SearchCriteria searchCriteria = new SearchCriteria();
				searchCriteria.setAttribute("user.id", user.getId());
				this.bannedUserList = (List<BannedUser>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannedUser.class);
			}
		} else {
			this.bannedUserList = (List<BannedUser>) CommonFunctionDelegator.getAllfromtable(BannedUser.class.getName());
		}
		return SUCCESS;
	}

	public String unblockBlockUser() throws Exception {
		if(this.pageSize <= 0)
			this.pageSize = 10;

		if(request.getParameter("id") != null) {
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute("user.id", Integer.parseInt(request.getParameter("id")));
			this.bannedUserList = (List<BannedUser>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannedUser.class);

			if(this.bannedUserList.size() > 0) {
				CommonFunctionDelegator.GeneralDelete(this.bannedUserList.get(0));
			}
		}

		return execute();
	}

	public String addBlockUser() throws Exception {

		if(user.equalsIgnoreCase("") ){
			addActionError("Enter user id for block user");
			this.bannedUserList = (List<BannedUser>) CommonFunctionDelegator.getAllfromtable(BannedUser.class.getName());
			return INPUT;
		}
		User userObj = UserServiceDelegator.getUserByUserId(user);

		if(userObj == null){
			addActionError("User with this user id was not found");
			this.bannedUserList = (List<BannedUser>) CommonFunctionDelegator.getAllfromtable(BannedUser.class.getName());
			return INPUT;
		}
		SearchCriteria searchCriteria = new SearchCriteria();
		searchCriteria.removeAllAttribute();
		searchCriteria.setAttribute("user", userObj);
		this.bannedUserList = (List<BannedUser>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannedUser.class);

		if(bannedUserList != null && bannedUserList.size() > 0) {
			addActionError("User already added to blocked list");
		}else{
			this.bannedUser.setUser(userObj);
			CommonFunctionDelegator.generalInsertbyobj(this.bannedUser);
			addActionMessage("User added to blocked list");
		}

		this.bannedUserList = (List<BannedUser>) CommonFunctionDelegator.getAllfromtable(BannedUser.class.getName());
		return SUCCESS;
	}

	public List<BannedUser> getBannedUserList() {
		return bannedUserList;
	}

	public String getFindUser() {
		return findUser;
	}

	public void setFindUser(String findUser) {
		this.findUser = findUser;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
